package org.lds.hotkey.client.autoconfigure.controller;

import org.lds.hotkey.client.util.HotKeyUtil;
import org.lds.hotkey.common.model.ReportRuleModel;
import org.lds.hotkey.common.model.dto.ResultDTO;
import org.lds.hotkey.worker.remote.model.ReceiveRuleModelRequest;
import org.lds.hotkey.worker.remote.util.WorkerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test/api")
public class TestController {
    @RequestMapping("/hotkey/{key}")
    public ResultDTO<String> hotKey(@PathVariable String key) {
        boolean hotKey = HotKeyUtil.isHotKey(key);
        if (hotKey) {
            HotKeyUtil.put(key, "hello");
            String value = (String) HotKeyUtil.getCacheValue(key);
            return ResultDTO.success(value);
        }
        return ResultDTO.success("false");
    }

    @RequestMapping("/worker/push")
    public ResultDTO<Boolean> pushWorker() {
        String address = "127.0.0.1:7091";
        String key = "sso:cache:client";
        ReceiveRuleModelRequest request = ReceiveRuleModelRequest.builder()
                .reportRuleModel(ReportRuleModel.builder()
                        .ruleId(0L)
                        .key(key)
                        .build())
                .build();
        return WorkerClient.receiveRuleModel(address, request);
    }
}
